home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 10.0 KB | 383 lines | [TEXT/MPS ] |
- /*
- File: UAppleEvents.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __UAPPLEEVENTS__
- #include "UAppleEvents.h"
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __BUFFER__
- #include "Buffer.h"
- #endif
-
- #ifndef __STRING__
- #include <String.h>
- #endif
-
- #ifndef __OBJECTLIST__
- #include "ObjectList.h"
- #endif
-
- #ifndef __NEWDELETE__
- #include "NewDelete.h"
- #endif
-
- /***********************************|****************************************/
-
- struct MAEventTableRec
- {
- OSType theClass;
- OSType theID;
- long theValue;
- };
-
- typedef MAEventTableRec* MAEventTablePointer;
-
- //--------------------------------------------------------------------------------------------------
- // Globals
-
- AEIdleUPP gAppleEventIdleProc = NULL;
- AEFilterUPP gAppleEventFilterProc = NULL;
- AEEventHandlerUPP gAppleEventsDispatchUPP;
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment Initialize
-
- void InitUAppleEvents(ProcPtr dispatcher)
- {
- short numberOfTables = Count1Resources('aedt');// Count the number of 'aedt' resources
- gAppleEventsDispatchUPP = (AEEventHandlerUPP) NewRoutineDescriptor((ProcPtr)dispatcher, uppAEEventHandlerProcInfo, GetCurrentISA());
-
- for (short tableIndex = 1; tableIndex <= numberOfTables; ++tableIndex)
- {
- Handle tableHandle = Get1IndResource('aedt', tableIndex);
- HLock(tableHandle);
- Size tableSize = GetHandleSize(tableHandle);
- short tableElements = (short)(tableSize / sizeof(MAEventTableRec));
- MAEventTablePointer tablePtr = (MAEventTablePointer) * tableHandle;
-
- for (short eventIndex = 1; eventIndex <= tableElements; ++eventIndex)
- {
- // Install a single event handler for all events
- OSErr err = AEInstallEventHandler(tablePtr->theClass, tablePtr->theID, gAppleEventsDispatchUPP, tablePtr->theValue, false);
- ++tablePtr;
- }
-
- ReleaseResource(tableHandle);
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AppleEvent
-
- TAppleEvent::TAppleEvent
- (
- const AEEventClass itsEventClass,
- const AEEventID itsEventID,
- const AEAddressDesc& itsAddress,
- long itsSendingMode
- )
- {
- fSendingMode = itsSendingMode;
- fPriority = kAENormalPriority;
- fTimeoutVal = kAEDefaultTimeout;
- fFreeMessage = true;
- AECreateAppleEvent(itsEventClass, itsEventID, &itsAddress, kAutoGenerateReturnID, kAnyTransactionID, &fMessage);
- }
-
- //--------------------------------------------------------------------------------------------------
-
- TAppleEvent::TAppleEvent(const AppleEvent& theMessage, Boolean freeMessage)
- {
- fSendingMode = kAENoReply;
- fPriority = kAENormalPriority;
- fTimeoutVal = kAEDefaultTimeout;
- fMessage = theMessage;
- fFreeMessage = freeMessage;
- }
-
- //--------------------------------------------------------------------------------------------------
-
- TAppleEvent::~TAppleEvent()
- {
- if (fFreeMessage)
- AEDisposeDesc(&fMessage);
- }
-
- //--------------------------------------------------------------------------------------------------
-
- TAppleEvent* TAppleEvent::Send(OSErr& err)
- {
- AppleEvent theReply;
- TAppleEvent* result = NULL;
-
- err = AECreateDesc(typeNull, NULL, 0, &theReply);
-
- if (err != noErr)
- return nil;
-
- err = AESend(&fMessage, &theReply, fSendingMode, fPriority, fTimeoutVal, gAppleEventIdleProc, gAppleEventFilterProc);
-
- if (err != noErr)
- return nil;
-
- if (fSendingMode == kAEWaitReply)
- {
- // If we were waiting for a reply to our AppleEvent, instantiate a
- // TAppleEvent and initialize it with theReply.
- result = new TAppleEvent(theReply, true);
-
- if (result == nil)
- {
- AEDisposeDesc(&theReply);
- err = mFulErr;
- }
- }
- else
- AEDisposeDesc(&theReply);
-
- return result;
- }
-
- //--------------------------------------------------------------------------------------------------
-
- OSErr TAppleEvent::GetReturnID(long& returnID)
- {
- DescType actualType;
- long actualSize;
- return AEGetAttributePtr(&fMessage, keyReturnIDAttr, typeLongInteger, &actualType, (Ptr) & returnID, sizeof(returnID), &actualSize);
- }
-
- //--------------------------------------------------------------------------------------------------
-
- OSErr TAppleEvent::GetTransactionID(long& transactionID)
- {
- DescType actualType;
- long actualSize;
- return AEGetAttributePtr(&fMessage, keyTransactionIDAttr, typeLongInteger, &actualType, (Ptr) & transactionID, sizeof(transactionID), &actualSize);
- }
-
- //--------------------------------------------------------------------------------------------------
-
- OSErr TAppleEvent::ReadShort(const AEKeyword theKey, short& theValue)
- {
- DescType actualType;
- long actualSize;
- theValue = 0;
- return AEGetParamPtr(&fMessage, theKey, typeShortInteger, &actualType, (Ptr) & theValue, sizeof(theValue), &actualSize);
- }
-
- //--------------------------------------------------------------------------------------------------
-
- OSErr TAppleEvent::ReadLong(const AEKeyword theKey, long& theValue)
- {
- DescType actualType;
- long actualSize;
- theValue = 0;
-
- return AEGetParamPtr(&fMessage, theKey, typeLongInteger, &actualType, (Ptr) & theValue, sizeof(theValue), &actualSize);
- }
-
- //--------------------------------------------------------------------------------------------------
-
- OSErr TAppleEvent::ReadString(const AEKeyword theKey, char* theData)
- {
- DescType actualType;
- long actualSize;
-
- OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeChar, &actualType, (Ptr) theData, 255, &actualSize);
-
- if (theErr == noErr)
- {
- if (actualSize > 255)
- actualSize = 255;
-
- theData[actualSize] = 0;
- }
-
- return theErr;
- }
-
- //--------------------------------------------------------------------------------------------------
-
- OSErr TAppleEvent::ReadParameter (const AEKeyword theKey, StringPtr str)
- {
- DescType actualType;
- long actualSize;
-
- OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeChar, &actualType, (Ptr) & str[1], 255, &actualSize);
-
- if (theErr == noErr)
- str[0] = (unsigned char) actualSize;
- else
- str[0] = 0;
-
- return theErr;
- }
-
- /***********************************|****************************************/
-
- OSErr TAppleEvent::ReadParameter ( const AEKeyword key, ADataItem& value )
- {
- // First, get a handle to the data for this AppleEvent.
- AEDesc dataDescriptor;
-
- OSErr result = AEGetParamDesc ( & fMessage, key, typeWildCard, & dataDescriptor );
-
- if ( result == noErr )
- {
- // Lock down the data.
- HLock ( dataDescriptor.dataHandle );
-
- if ( value.ReadFrom ( * dataDescriptor.dataHandle, GetHandleSize ( dataDescriptor.dataHandle),
- dataDescriptor.descriptorType ) == GetHandleSize( dataDescriptor.dataHandle ) )
- result = noErr;
- else
- result = memFullErr;
-
- AEDisposeDesc ( & dataDescriptor );
- }
- return result;
- }
-
- /***********************************|****************************************/
-
- OSErr TAppleEvent::ReadList ( const AEKeyword theKey, const DescType theType, TObjectList& list )
- {
- AEDescList theDescList;
- OSErr theErr = AEGetParamDesc(&fMessage, theKey, typeAEList, &theDescList);
-
- if (theErr == noErr)
- {
- char buffer[256];
- AEKeyword theActualKey;
- long items;
- long actualSize;
- DescType theActualType;
-
- theErr = AECountItems(&theDescList,&items);
-
- if (theErr != noErr)
- return theErr;
-
- for (short index = 1; index <= items; ++index)
- {
- theErr = AEGetNthPtr(&theDescList, index, theType, &theActualKey,&theActualType, (Ptr) &buffer,sizeof(buffer),&actualSize);
-
- if (theErr != noErr)
- return theErr;
-
- list.Append ( new CBuffer ( buffer, actualSize ) );
- }
- }
-
- return theErr;
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TAddressDescription::TAddressDescription ():
- TDirectObject (),
- fAdoptData ( true )
- {
- fData.descriptorType = '\?\?\?\?';
- fData.dataHandle = nil;
- }
-
- /***********************************|****************************************/
-
- TAddressDescription::TAddressDescription ( DescType type, const void* data, unsigned long length ):
- TDirectObject (),
- fAdoptData ( true )
- {
- fData.descriptorType = type;
- fData.dataHandle = ::AllocateHandle ( length );
-
- if ( fData.dataHandle )
- ::BlockMove ( data, *fData.dataHandle, length );
- }
-
- /***********************************|****************************************/
-
- TAddressDescription::TAddressDescription ( const AEDesc& description, Boolean adoptData ):
- TDirectObject (),
- fData ( description ),
- fAdoptData ( adoptData )
- {
- }
-
- /***********************************|****************************************/
-
- TAddressDescription::TAddressDescription ( const TAddressDescription& that ):
- TDirectObject ( that ),
- fAdoptData ( true )
- {
- fData.descriptorType = that.fData.descriptorType;
- fData.dataHandle = that.fData.dataHandle;
-
- if ( fData.dataHandle )
- ::HandToHand ( &fData.dataHandle );
- }
-
- /***********************************|****************************************/
-
- TAddressDescription::~TAddressDescription ()
- {
- if ( fAdoptData && fData.dataHandle )
- ::DeallocateHandle ( fData.dataHandle );
- }
-
- /***********************************|****************************************/
-
- TAddressDescription&
- TAddressDescription::operator = ( const TAddressDescription& that )
- {
- TDirectObject::operator = ( that );
-
- if ( fAdoptData && fData.dataHandle )
- ::DeallocateHandle ( fData.dataHandle );
-
- fData.descriptorType = that.fData.descriptorType;
- fData.dataHandle = that.fData.dataHandle;
-
- if ( fData.dataHandle )
- ::HandToHand ( &fData.dataHandle );
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TAddressDescription::operator == ( const TAddressDescription& that ) const
- {
- return fData.descriptorType == that.fData.descriptorType;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TAddressDescription::operator != ( const TAddressDescription& that ) const
- {
- return fData.descriptorType != that.fData.descriptorType;
- }
-
- /***********************************|****************************************/